package com.bcxj.servicemap.remote;

import com.bcxj.internalcommon.constant.AmapConfigConstants;
import com.bcxj.internalcommon.dto.PointDTO;
import com.bcxj.internalcommon.dto.PointRequest;
import com.bcxj.internalcommon.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URL;

/**
 * ClassName: PointClient
 * Package: com.bcxj.servicemap.remote
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/11/2 18:13
 * @Version 1.0
 * @Since 1.0
 */
@Component
public class PointClient {

    @Value("${amap.key}")
    private String key;

    @Value("${amap.sid}")
    private String sid;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 增加轨迹点
     * @param pointRequest
     * @return
     */
    public ResponseResult upload (PointRequest pointRequest) {
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.POINT_UPLOAD_URL);
        url.append("?key=").append(key);
        url.append("&sid=").append(sid);
        url.append("&tid=").append(pointRequest.getTid());
        url.append("&trid=").append(pointRequest.getTrid());
        url.append("&points=");
        url.append("%5B");
        // 对于参数里面有特殊的要求的时候，对应的符号建议使用%数字的 形式展示
        for (PointDTO point : pointRequest.getPoints()) {
            url.append("%7B");
            url.append("%22location%22:");
            url.append("%22" + point.getLocation() + "%22");
            url.append(",%22locatetime%22:");
            url.append(point.getLocatetime());
            url.append("%7D");
            if(point != pointRequest.getPoints()[pointRequest.getPoints().length - 1]) {
                url.append(",");
            }
        }
        url.append("%5D");
        System.out.println(url);

        ResponseEntity<String> objectResponseEntity = restTemplate.postForEntity(URI.create(url.toString()), null, String.class);

        System.out.println(objectResponseEntity.getBody());
        return ResponseResult.success();

    }

}
